package com.play.ballen.me.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.play.ballen.R

class OperateSuccessDialogUtils(private val mContext: Context) {

    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var tvContent: TextView

    fun show(text: String) {
        if (!this::contentView.isInitialized) {
            contentView =
                LayoutInflater.from(mContext).inflate(R.layout.dialog_operate_success, null)
            contentView.let {

                tvContent = it.findViewById(R.id.tvContent)
                it.findViewById<View>(R.id.btnOk).setOnClickListener { dialog.dismiss() }

                dialog.setContentView(it)
                dialog.setCanceledOnTouchOutside(false)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                dialog.window?.setBackgroundDrawable(ColorDrawable())
            }
        }

        tvContent.text = text

        if (!dialog.isShowing) {
            dialog.show()
        }
    }
}